#pragma once

template <class _T>
class CMenu3
{
    _T* m_lpWnd;

public:
    CMenu3() { }
    CMenu3(_T* lpWnd) : m_lpWnd(lpWnd) { }
    virtual ~CMenu3() { }

public:
    BEGIN_MSG_MAP(CMenu3)
        ALT_MSG_MAP(1)
        COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen)
        COMMAND_ID_HANDLER(ID_FILE_EXIT, OnFileExit)
        ALT_MSG_MAP(2)
        COMMAND_ID_HANDLER(ID_FILE_OPEN, OnFileOpen2)
        COMMAND_ID_HANDLER(ID_FILE_EXIT, OnFileExit)
    END_MSG_MAP()

    LRESULT OnFileOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
        MessageBox(nullptr, _T("FileOpen"), _T("File"), MB_OK);
        return 0;
    }

    LRESULT OnFileOpen2(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
        MessageBox(nullptr, _T("FileOpen2"), _T("File"), MB_OK);
        return 0;
    }

    LRESULT OnFileExit(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
    {
        m_lpWnd->DestroyWindow();
        return 0;
    }
};

class CWinTest3 : public CWindowImpl<CWinTest3>
{
    CMenu3<CWinTest3> m_Menu;

public:
    CWinTest3() : m_Menu(this) { }
    virtual ~CWinTest3() { }

public:
    BEGIN_MSG_MAP(CWinTest3)
        MESSAGE_HANDLER(WM_DESTROY, OnDestroyHandler)

    //
    //  {
    //      if (theChainMember.ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, msgMapID))
    //          return TRUE;
    //  }
    //

        CHAIN_MSG_MAP_ALT_MEMBER(m_Menu, 2)
    END_MSG_MAP()

protected:
    LRESULT OnDestroyHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
        PostQuitMessage(0);
        return TRUE;
    }
};
